package com.wyyc.common.car.service.impl;

import com.alibaba.fastjson.JSON;
import com.wyyc.common.car.repository.CarRepository;
import com.wyyc.common.car.service.CarService;
import com.wyyc.entity.MyCarEntity;
import com.wyyc.entity.ResJson;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.List;

/**
 * Created by Hwei~ on 2017/6/7.
 */
@Service
public class CarServiceImpl implements CarService {

    @Resource
    private CarRepository carRepository;

    @Override
    public ResJson getMyCar(int uid) {
        ResJson resJson = new ResJson();
        List<MyCarEntity> myCarEntityList = carRepository.findByUid(uid);
        resJson.setStatus(0);
        resJson.setDesc("用户车辆信息查询成功!");
        resJson.setResult(JSON.toJSONString(myCarEntityList));
        return resJson;
    }

    @Override
    public ResJson addMyCar(int uid,String hphm, String cityCode, String classno, String engineno, String name, String phone) {
        ResJson resJson = new ResJson();
        MyCarEntity myCarEntity = new MyCarEntity();
        myCarEntity.setUserId(uid);
        myCarEntity.setHphm(hphm);
        myCarEntity.setOwner(name);
        myCarEntity.setPhone(phone);
        myCarEntity.setClassno(classno);
        myCarEntity.setEngineno(engineno);
        myCarEntity.setAddate(new Date(System.currentTimeMillis()));
        myCarEntity.setCreateTime(new Timestamp(System.currentTimeMillis()));
        myCarEntity.setFen(0);
        myCarEntity.setIllegal(0);
        myCarEntity.setMoney(0.00);
        myCarEntity.setHpzl("02");
        myCarEntity.setCityCode(cityCode);
        try{
            carRepository.save(myCarEntity);
        }catch (Exception e){
            resJson.setStatus(1);
            resJson.setDesc("用户车辆添加失败!");
            return resJson;
        }
        resJson.setStatus(0);
        resJson.setDesc("用户车辆添加成功!");
        return resJson;
    }
}
